package utils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import commands.ICommand;

public class Clock
{
	private static int nbTempoPerMeasure;
	private static int currentTempo;
	private static Timer tempoTimer;
	private static ICommand tempoLedCommand;
	private static ICommand measureLedCommand;
	
	public static void StartBattreTempo(ICommand tempoLedCommand, ICommand measureLedCommand, float tempo, int nbTempoPerMeasure)
	{
		Clock.nbTempoPerMeasure = nbTempoPerMeasure;
		Clock.currentTempo = 0;
		Clock.tempoLedCommand = tempoLedCommand;
		Clock.measureLedCommand = measureLedCommand;
		int delay = (int)((60/tempo)*1000);
		
		if(tempoTimer != null)
			StopBattreTempo();
			
		tempoTimer = new Timer(delay, new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Clock.tempoLedCommand.execute();
				BattreMeasure();
			}
		});
		
		tempoTimer.setRepeats(true);
		tempoTimer.start();
	}
	public static void StopBattreTempo()
	{
		tempoTimer.stop();
	}
	
	private static void BattreMeasure()
	{
		currentTempo++;

		if(currentTempo == nbTempoPerMeasure)
		{
			measureLedCommand.execute();
			currentTempo = 0;
		}
	}
}